﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;

namespace DataAssociations
{
    public partial class OptionsForm : Form
    {
        public OptionsForm()
        {
            InitializeComponent();
            _optionsFileReader = new OptionsFileReader();
            var options = _optionsFileReader.Options;
            comboBoxAlgorithm.Text = options.ComboBoxAlgorithm;
            textBoxPath.Text = options.TextBoxPath;
            textBoxSupport.Text = options.TextBoxSupport.ToString();
            textBoxConfidence.Text = options.TextBoxConfidence.ToString();
            if (!options.IsApriori)
            {
                textBoxSupport.Enabled = false;
            }
        }

        private readonly OptionsFileReader _optionsFileReader;

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            var options = new RunOptions();
            options.ComboBoxAlgorithm = comboBoxAlgorithm.Text;
            options.TextBoxPath = textBoxPath.Text;
            if (options.IsApriori)
            {
                int support;
                if (!Int32.TryParse(textBoxSupport.Text, out support))
                {
                    MessageBox.Show("Support must be a number", "Enter a number");
                    return;
                }
                options.TextBoxSupport = support;
            }
            int confidence;
            if (!Int32.TryParse(textBoxConfidence.Text, out confidence))
            {
                MessageBox.Show("Confidence must be a number", "Enter a number");
                return;
            }
            options.TextBoxConfidence = confidence;

            _optionsFileReader.Persist(options);
            Close();
        }

        private void comboBoxAlgorithm_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxAlgorithm.SelectedItem.ToString().Contains("Apriori"))
                textBoxSupport.Enabled = true;
            else
                textBoxSupport.Enabled = false;
        }
    }
}
